<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="com.google.appengine.api.users.User"%>
<%@ page import="com.google.appengine.api.users.UserService"%>
<%@ page import="com.google.appengine.api.users.UserServiceFactory"%>
<%@ page import="org.teammach.laddercomp.dao.LadderManager"%>
<%@ page import="org.teammach.laddercomp.dao.BlogManager"%>

<%@ page import="org.teammach.laddercomp.shared.Ladder"%>
<%@ page import="org.teammach.laddercomp.shared.OfficialLadder"%>
<%@ page import="org.teammach.laddercomp.shared.Team"%>
<%@ page import="org.teammach.laddercomp.shared.BlogEntry"%>

<%@ page import="com.google.appengine.api.datastore.Entity"%>
<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>
<%@ page import="java.text.SimpleDateFormat"%>


<%@ page import="java.util.List"%>
<%@ page import="java.util.TimeZone"%>


<html>
<head>
<link type="text/css" rel="stylesheet" href="style.css">
<script type="text/javascript">

 var _gaq = _gaq || [];
 _gaq.push(['_setAccount', 'UA-23998388-1']);
 _gaq.push(['_trackPageview']);

 (function() {
   var ga = document.createElement('script'); ga.type =
'text/javascript'; ga.async = true;
   ga.src = ('https:' == document.location.protocol ? 'https://ssl' :
'http://www') + '.google-analytics.com/ga.js';
   var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
 })();

</script>
</head>
<body>
<div id="wrapper">

<a href="/">
<span id="header">
</span>
</a>
<%
	UserService userService = UserServiceFactory.getUserService();
	User user = userService.getCurrentUser();
	LadderManager manager = LadderManager.INSTANCE;
	boolean adminUser = false;
	if (user != null
			&& (user.getEmail().equalsIgnoreCase("dscerri@gmail.com") || user
					.getEmail().equalsIgnoreCase("haydns33@gmail.com"))) {
		adminUser = true;
	}
		%>

<div id="page">

<div style="float:left;width:480px">
<div id="content" style="padding:10px;width:460px">
<h1 class="title">Haydn's Blog</h1>


<% BlogManager blogManager = BlogManager.INSTANCE; 

List<BlogEntry> blogEntries = blogManager.getAllBlogs();

if(blogEntries.size() == 0) {
	%>
		There are no blog entries currently available	
	<%
} else {
	SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy hh:mmaaa");
	sdf.setTimeZone(TimeZone.getTimeZone("GMT+10"));
			
	
	for(BlogEntry entry : blogEntries) {
	%>
	
	<span style="font-size:22px"><%=entry.getTitle() %> </span>
	
	<span class="meta">
	 <%= sdf.format(entry.getCreated()) %></span>
	 <% if(adminUser){ %>
	 <div style="text-align:right;font-size:10px;color:#646464">
	    <a href="blog.jsp?edit=<%=KeyFactory.keyToString(entry.getKey()) %>">EDIT</a> 
	    <a href="updateBlog.jsp?blogKey=<%=KeyFactory.keyToString(entry.getKey())%>&actionType=delete">DELETE</a> 
	  </div>
	<%} %>
	<p><%=entry.getContent() %></p>
	
	<% 
	if(adminUser) {

	if(request.getParameter("edit") != null && request.getParameter("edit").equalsIgnoreCase(KeyFactory.keyToString(entry.getKey()))) { %>
	<form method=post action="updateBlog.jsp">   
          Title <input type="text" name="title" value="<%= entry.getTitle() %>"></input><br> 
          Content <br>
    <textarea name="content" rows="10" cols="50"><%=entry.getContent().toString() %> </textarea>
    <input type="hidden" name="blogKey" value="<%=KeyFactory.keyToString(entry.getKey()) %>">
    <input type="hidden" name="actionType" value="edit">
    <P><INPUT TYPE=SUBMIT>
    </FORM>
	
	
	<%
	}

	}
}
}
%>

<%	if (adminUser) {
	%>


<FORM METHOD=POST ACTION="addBlog.jsp">
<h1 class="title">Add new entry</h1>
Title <input type="text" name="title" value=""></input><br>
Content <br>
<textarea name="content" rows="10" cols="50">
</textarea>
<P><INPUT TYPE=SUBMIT>
</FORM>

<%} %>


</div>
</div>

<div style="float:right;width:250px">

<div id="sidebar" style="height:613px">
	<img src="haydnOldScaled.jpg"></img>
</div>
	
	<div id="sidebar">
<h1 class="title">Links</h1>



	<div  style="text-align:center;margin-top:-10px;padding:5px">
		<ul>
			<li><a href="standings.jsp">Current Standings</a></li>
			<li><a href="rules.jsp">Rules and regulations</a> </li>
			<li><a href="blog.jsp">Haydn's weekly blog</a> </li>
			<li><a href="mailto:dscerri@gmail.com">Contact Webmaster</a> </li>
				<%
		if (user != null
				&& (user.getEmail().equalsIgnoreCase("dscerri@gmail.com") || user
						.getEmail().equalsIgnoreCase("haydns33@gmail.com"))) {
	%>
	<li><a href="<%=userService.createLogoutURL(request.getRequestURI())%>">Log out</a>	</li><%
		} else {
	%>
	<li><a href="<%=userService.createLoginURL(request.getRequestURI())%>">Admin login</a></li>
	<%
		}
	%>
		</ul>
	</div>
	</div>
	</div>
			
	</div>
	
	</div>
	<div id="footer">
	<p>&copy;2011 All Rights Reserved &nbsp;&bull;&nbsp; Design by Dave
	Scerri Productions &nbsp;&bull;&nbsp; CSS Template from <a
		href="http://www.freecsstemplates.org/">Free CSS Templates</a> .</p>
	</div>
</body>
</html>